/**CopyRight[1990-2012]<lyc2877@gmail.com>
 */
#include "./include/Worker.h"
int32_t CWorker::ui_count = 0;
pthread_mutex_t CWorker::m_lock = PTHREAD_MUTEX_INITIALIZER;

CWorker::CWorker(int64_t in_lType)
{
  m_pMQueue = new CMQueue(in_lType);
}

CWorker::~CWorker()
{
  if (NULL != m_pMQueue) {
    delete m_pMQueue;
  }
}

int32_t CWorker::work()
{
  TMsg t_msg;
  int status = 0;
  while(1)
  {
    m_pMQueue->recv_msg(&t_msg);
    status = *(int*)t_msg.m_szBuf;
    if(status < 10)
    {
      ++status;
      memcpy(t_msg.m_szBuf, &status, sizeof(int));
      for(int i = 0; i< 5000; i++)
      {
      }
      m_pMQueue->send_msg(&t_msg);
    }
    else {
      pthread_mutex_lock(&CWorker::m_lock);
      ++CWorker::ui_count;
      if (0 == CWorker::ui_count % 10000) {
        char szBuf[30];
        print_time(szBuf);
        printf("now[%s] count[%d]\n",
               szBuf,
               CWorker::ui_count);
      }
      pthread_mutex_unlock(&CWorker::m_lock);
    }
  }
  return 0;
}
